package com.xxxx.comment.utils;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Bean相关的工具类
 *
 * @author lzf
 * @date 2023/8/29 16:29:56
 */
public class BeanCopyUtils
{
    public static <V> V copyBean(Object source, Class<V> targetClass)
    {
        V target;
        try
        {
            target = targetClass.newInstance();
            BeanUtils.copyProperties(source, target);
        }
        catch (Exception e)
        {
            throw new RuntimeException();
        }
        return target;
    }
    
    public static <V> List<V> copyBeanList(List<Object> sourceList, Class<V> targetClass)
    {
        
        return sourceList.stream()
                .map(source -> copyBean(source, targetClass))
                .collect(Collectors.toList());
    }
}
